<?php
class Model_Order extends Zend_Db_Table_Abstract
{
    protected $_name = 'order';

    public function getPaginator($filters = array(), $sort = null, $page = 1, $rowCount = null)
    {
        // get select query
        $select = $this->select ();
        if ($filters != null)
        {
            if (is_array ( $filters ))
            {
                foreach ( $filters as $key => $val )
                    $select->where ( $key, $val );
            }
            else
            {
                $select->where ( $filters );
            }
        }
        
        if ($sort != null)
            $select->order ( $sort );
            
        // get adapter
        $adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
        
        // get paginator
        $paginator = new Zend_Paginator ( $adapter );
        $paginator->setItemCountPerPage ( $rowCount );
        $paginator->setCurrentPageNumber ( $page );
        
        return $paginator;
    }

    public function getList($filters = array(), $sort = null, $page = null, $rowCount = null)
    { 
        $select = $this->select ();
        if ($filters != null)
        {
            if (is_array ( $filters ))
            {
                foreach ( $filters as $key => $val )
                    $select->where ( $key, $val );
            }
            else
            {
                $select->where ( $filters );
            }
        }
        
        if ($page != null)
            $select->limitPage ( $page, $rowCount );
        
        if ($sort != null)
            $select->order ( $sort );
            
        return $this->fetchAll ( $select );
    }
    
    public function createOrder($memberID)
    {
        // create a new row in the table
        $row = $this->createRow ();
       
        // set the row data
        $row->member_id = $memberID;
        
        $dateObject = Zend_Date::now ();
        $row->date = $dateObject->toString("YYYY-MM-dd HH:mm:ss");
        
        // save the new row
        $row->save ();
        
        // now fetch the id of the row you just created and return it 
        $id = $row->order_id; //$this->_db->lastInsertId();
        return $id;
    }
    
    public function deleteOrder($id)
    {
        // find the row that matches the id
        $row = $this->find ( $id )->current ();
        if ($row)
        {
            $row->delete ();
            return true;
        }
        else
        {
            throw new Zend_Exception ( "Delete function failed; could not find row!" );
        }
    }
    
    public function getManageOrders($orderID, $fullname, $date)
    {
    	$sql = "SELECT `order`.order_id, members.fullname, order_total.product_number, order_total.total, order.date FROM `order` LEFT JOIN `members` ON order.member_id = members.id 
											LEFT JOIN (SELECT order.order_id, COUNT(order_detail.order_id) AS product_number, SUM(order_detail.quantity*order_detail.price) AS total FROM `order` LEFT JOIN order_detail ON order.order_id = order_detail.order_id GROUP BY order.order_id) AS order_total ON order.order_id = order_total.order_id";
    	
    	$where = array();
    	if($orderID) {
    		$where[] = " order.order_id = ".$orderID;
    	} 
    	if($fullname) {
    		$where[] = " members.fullname like '%".$fullname."%'";
    	}
    	
    	if(count($where))
    	{
    		$sql .= " where ". implode(" and ", $where);
    	}
    	
    	$query = $this->getAdapter()->query($sql);
    	$records = $query->fetchAll();
    	 
    	return $records;
    }
    
    public function getOrderDetails($orderID)
    {
    	$sql = "SELECT * FROM order_detail LEFT JOIN product ON order_detail.product_id = product.product_id WHERE order_detail.order_id = ".$orderID;
    	 
    	$query = $this->getAdapter()->query($sql);
    	$records = $query->fetchAll();
    	
    	return $records;
    }
}
?>
